package com.ljcalc.business.components.calcs;

import java.util.*;

import com.ljcalc.business.components.*;

public class FuErDaiCombCalc extends CombCalc {

	/**
	 * 
	 * @param n
	 * @param list
	 * @return
	 */
	protected LinkedList<NumberPercentage> getRandomNPFromSections(
			LinkedList<NumberPercentage> list) {

		LinkedList<NumberPercentage> retList = new LinkedList<NumberPercentage>();

		// sort the list
		list = sortListWthPercentage(list);
		// divide the list into five sections

		// from the sections get np elements out by using
		// special algorithm, and add it to the return list
		/*
		 * The special algorithm is: we have five sections, every section will
		 * have seven numbers, from the most occurrence section we get 5 number
		 * percentage out From the 2nd most ...... section we get 4 number
		 * percentage out From the 3nd most ...... section we get 3 number
		 * percentage out From the 4nd most ...... section we get 2 number
		 * percentage out From the 5nd most ...... section we get 1 number
		 * percentage out
		 * 
		 * for 35 numbers with 5 sections,we need random number from 1 to 7,
		 * when we need 5 numbers we iterate 5 times, etc. which means in the
		 * prefilling process, we will have around 47 numbers filled So the comb
		 * set number has to larger than 47/7, which is more than 7
		 */

		RandomPool rp = null;
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < (5 - i); j++) {
				rp = RandomPoolManager.getInstance().getRandomPool(1, 7);
				int r = (int) (rp.getNextRandomValue() + ((4 - i) * 7));
				if (!retList.contains(list.get(r - 1))) {
					retList.add(list.get(r - 1));
				} else {
					j--;
				}
			}
		}

		return retList;
	}

	@Override
	protected long calcNumberOfCombinations(long percentage,
			long totalNumberOfSamples) {
		return percentage * totalNumberOfSamples / 1000;
	}

	@Override
	public CombinationsSet prefilling() {
		return super.prefilling();
	}

	@Override
	public CombinationsSet fillUp(CombinationsSet combset) {
		CombinationsSet retSet = super.fillUp(combset);

		Iterator<Combination> itr = retSet.iterator();
		while (itr.hasNext()) {
			Combination comb = (Combination) itr.next();
			comb.setHasSameCombInHistory(analyzerInstance.hasSameCombInHistory(comb));

			comb.setisConsecutiveAcceptableCombination(analyzerInstance
							.isConsecutiveAcceptableCombination(comb));
			comb.setInTimeConsecutiveTolerance(analyzerInstance.isInTimeConsecutiveTolerance(comb));
			
			comb.setHistorySimilarities(analyzerInstance.getHistorySimilarities(comb,5));
			
			comb.setAverageSubCombOccurencesTimes(analyzerInstance.getAverageSubCombOccurenceTimes(comb));
			
			comb.setIsMinMaxDiffTolerance(analyzerInstance.isInMinMaxDiffTolerance(comb));
			
			comb.setIsMinMaxMidDiffTolerance(analyzerInstance.isInMinMaxDiffMidTolerance(comb));
			
		}

		return retSet;

	}

	@Override
	protected LinkedList<NumberPercentage> getReducedNPList(
			LinkedList<NumberPercentage> npList) {
		// TODO Auto-generated method stub
		return getRandomNPFromSections(npList);
	}

	public LinkedList<NumberPercentage> getReducedNPListTestHelper(
			LinkedList<NumberPercentage> npList) {
		// TODO Auto-generated method stub
		return getRandomNPFromSections(npList);
	}
}
